import FontStyle=require('./../kernel/obj/apperance/item/FontStyle');
import TextObj=require('./../kernel/obj/TextObj');
import DEFINE=require('./DEFINE');
import ParaContour=require('./graph/ParaContour');

 


export = TextContourServe;
    

/**
 *
 * @author 
 *
 */
class TextContourServe {
	public constructor() {
	}
	
	/**
	 * 获取文字轮廓
	 * 参数：text 文字类容
	 * 参数：fontStyle 文字样式，必须含有：fn/fs/cs/ls
	 * 参数：func 回调函数，形式如下：
	 *          function(pc:ParaContour)    
	 * [可选]参数：fz 文字轮廓的放大倍数
	 */ 
    public static get(text: string,fontStyle: FontStyle,func:Function,fz=TextObj.FONT_ZOOM_FACTOR) {
        console.log('TextContourServe para: '+text
            +'\nFONT: '+fontStyle.fn+', SIZE: '+fontStyle.fs+', CHAR_SPACE: '+fontStyle.cs+', LINE_SPACE: '+fontStyle.ls);

        // 网络通信
        var urlPara="?fontName="+fontStyle.fn
            +"&fontSize="+fontStyle.fs
            +"&fontZoom="+fz
            +"&boldItalic=0&position=1"
            +"&charSpace="+fontStyle.cs
            +"&lineSpace="+fontStyle.ls
            +"&text="+text;
        var url=DEFINE.FONT_SERVE_URL+urlPara;
        //        var url = "http://localhost:3002/contour" + urlPara;
        // var request: egret.HttpRequest=new egret.HttpRequest();
        var request: any=new egret.HttpRequest();
        var respHandler=function(evt: egret.Event): void {
            switch(evt.type) {
                case egret.Event.COMPLETE:
                    var request: egret.HttpRequest=evt.currentTarget;
                                        
                    // 解析返回信息
                    var retJson=JSON.parse(request.response);
                    if(retJson.success) {
                        var pc: ParaContour=ParaContour.fromFontServerData(text,retJson.data);
                        func(pc);
                    }else{
                        func(null);
                    }

                    console.log("ResponseHandler:\n"+request.response);
                    break;
                case egret.IOErrorEvent.IO_ERROR:
                    console.log("ResponseHandler IO Error!");
                    break;
            }
        }
        request.once(egret.Event.COMPLETE,respHandler,null);
        request.once(egret.IOErrorEvent.IO_ERROR,respHandler,null);
        request.open(url,egret.HttpMethod.GET);
        request.send("a=1");

    }
}
